<?php
namespace App\Admin\Controllers;
use App\Admin\Models\CollegeCarousel;
use App\Admin\Models\CollegeCategories;
use App\Admin\Models\CollegeColumn;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class CollegeController extends ControllerAdBase
{
	public function initialize()
	{
		parent::initialize();
	}

    /**
     * 商学院列表
     */
    public function clistAction(){
        $college_column = new CollegeColumn();
        $list = $college_column->find_list();
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> 100,
                "page" => 1
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }


    /**
     * 修改栏目
     */
    public function updatecAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'栏目分类id不能为空');
        }
        $collegeColumn = new CollegeColumn();
        $info = $collegeColumn->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该记录不存在，或已被删除');
        }
        $this->validate_field($this->params);
        $info['name']=$this->params['name'];
        $info['short_name']=$this->params['short_name'];
        $info['level_id']=$this->params['level_id'];
        $info['sort']=$this->params['sort'];
        $info['is_show']=$this->params['is_show'];
        $info['updated_at']=date('Y-m-d H:i:s');
        $info['type']=$this->params['type'];
        $save_column = new CollegeColumn();
        $result = $save_column->save_cloumn($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }
    }

    /**
     * 添加栏目
     */
    public function addcAction(){
        $this->validate_field($this->params);
        $info['name']=$this->params['name'];
        $info['short_name']=$this->params['short_name'];
        $info['level_id']=$this->params['level_id'];
        $info['sort']=$this->params['sort'];
        $info['is_show']=$this->params['is_show'];
        $info['type']=$this->params['type'];
        $info['updated_at']=date('Y-m-d H:i:s');
        $info['created_at']=date('Y-m-d H:i:s');
        $save_column = new CollegeColumn();
        $result = $save_column->save_cloumn($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }
    }


    public function validate_field($params){
        if(!isset($params['name']) || empty($params['name'])){
            $this->fail(-1,'栏目分类名称不能为空');
        }
        if(!isset($params['level_id']) || empty($params['level_id'])){
            $this->fail(-1,'等级不能为空');
        }
        if(!isset($params['sort']) || empty($params['sort'])){
            $this->fail(-1,'排序不能为空');
        }

    }

    public function delcAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'栏目分类id不能为空');
        }
        $collegeColumn = new CollegeColumn();
        $info = $collegeColumn->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该记录不存在，或已被删除');
        }
        $collegeCarousel = new CollegeCarousel();
        $count = $collegeCarousel->find_cout($this->params['id']);
        if($count){
            $this->fail(-1,'该栏目下面有教程，不能删除');
        }
        $result = $collegeCarousel->findFirstById($this->params['id'])->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }

    }


    public function calistAction(){
        $college_cate = new CollegeCategories();
        $list = $college_cate->find_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> 10,
                "page" => 1
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = ToolsController::paging($list_items);
        foreach($list_items['items'] as $key=>$value){
            $collegecolumn = new CollegeColumn();
            $column = $collegecolumn->find_by_id($value['cid']);
            $list_items['items'][$key]['cid_name']= $column['name'];
            $list_items['items'][$key]['type']= $column['type'];
        }
        $this->ok($list_items);
    }

    public function caaddAction(){
        $this->cavalidate_field($this->params);
        $info['name'] = $this->params['name'];
        $info['cid'] = $this->params['cid'];
        $info['sort'] = $this->params['sort'];
        $info['created_at'] = date('Y-m-d H:i:s',time());
        $info['updated_at'] = date('Y-m-d H:i:s',time());
        $info['is_show']=$this->params['is_show'];
        $college_cates = new CollegeCategories();
        $result = $college_cates->save_params($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }
    }


    public function caupdateAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'分类id不能为空');
        }
        $college_cate = new CollegeCategories();
        $info = $college_cate->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该分类不存在或以被删除');
        }
        $this->cavalidate_field($this->params);
        $info['name'] = $this->params['name'];
        $info['cid'] = $this->params['cid'];
        $info['sort'] = $this->params['sort'];
        $info['updated_at'] = date('Y-m-d H:i:s',time());
        $info['is_show']=$this->params['is_show'];
        $college_cates = new CollegeCategories();
        $result = $college_cates->save_params($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }

    }


    private function cavalidate_field($params){
        if(!isset($params['name']) || empty($params['name'])){
            $this->fail(-1,'分类名称不能为空');
        }
        if(!isset($params['cid']) || empty($params['cid'])){
            $this->fail(-1,'栏目不能为空');
        }
        if(!isset($params['sort']) || empty($params['sort'])){
            $this->fail(-1,'分类排序不能为空');
        }
    }


    public function cadelAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'栏目分类id不能为空');
        }
        $college_cate = new CollegeCategories();
        $info = $college_cate->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该分类不存在或以被删除');
        }
        $collegeCarousel = new CollegeCarousel();
        $count = $collegeCarousel->find_cate_cout($this->params['id']);
        if($count){
            $this->fail(-1,'该分类下面有教程，不能删除');
        }
        $college_cate = new CollegeCategories();
        $result = $college_cate->findFirstById($this->params['id'])->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }
    }


    /**
     * 获取图文列表
     */
    public function caroulistAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $collegeCarousel = new CollegeCarousel();
        $carousel_list = $collegeCarousel->find_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $carousel_list,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = ToolsController::paging($list_items);
        foreach($list_items['items'] as $key=>$value){
            $column_obj = new CollegeColumn();
            $column_info = $column_obj->find_by_id($value['cid']);
            $list_items['items'][$key]['cid_name']=$column_info['name'];
            $category_obj = new CollegeCategories();
            if($value['cateid']){
                $category = $category_obj->find_by_id($value['cateid']);
                $list_items['items'][$key]['cateid_name']=$category['name'];
            }else{
                $list_items['items'][$key]['cateid_name']=$column_info['name'];
            }
        }
        $this->ok($list_items);
    }

    public function child_listAction(){
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->fail(-1,'栏目分类id不能为空');
        }
        $college_columns = new CollegeColumn();
        $item_list = $college_columns->find_by_id($this->params['cid']);
        $college_category = new CollegeCategories();
        $child_list = $college_category->find_list(['cateid'=>$this->params['cid']]);
        $child_list=ToolsController::paging($child_list);
        $item_list['child_list']=$child_list;
        $item_list['frist_child']= $child_list ? $child_list[0]['id'] : 0;
        $this->ok($item_list);
    }


    public function carouaddAction(){
        $this->validate_fieldcarouup($this->params);
        $info['cateid']=$this->params['cateid'];
        $info['cid']=$this->params['cid'];
        $info['content']=$this->params['content'];
        $info['cover']=$this->params['cover'];
        $info['is_recommend']=$this->params['is_recommend'];
        $info['title']=$this->params['title'];
        $info['type']=$this->params['type'];
        $info['sort']=$this->params['sort'];
        $info['created_at'] = date('Y-m-d H:i:s',time());
        $info['updated_at'] = date('Y-m-d H:i:s',time());
        $info['sharecount']=$this->params['sharecount'];
        $info['studycount']=$this->params['studycount'];
        $info['zancount']=$this->params['zancount'];
        $info['collectcount']=$this->params['collectcount'];
        $collegeCou = new CollegeCarousel();
        $result = $collegeCou->save_params($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }


    }
    public function carouupdateAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'内容id不能为空');
        }
        $this->validate_fieldcarouup($this->params);
        $college_caroual = new CollegeCarousel();
        $info = $college_caroual->find_by_id($this->params['id']);
        $info['cateid']=$this->params['cateid'];
        $info['cid']=$this->params['cid'];
        $info['content']=$this->params['content'];
        $info['cover']=$this->params['cover'];
        $info['is_recommend']=$this->params['is_recommend'];
        $info['title']=$this->params['title'];
        $info['type']=$this->params['type'];
        $info['sort']=$this->params['sort'];
        $info['updated_at'] = date('Y-m-d H:i:s',time());
        $info['sharecount']=$this->params['sharecount'];
        $info['studycount']=$this->params['studycount'];
        $info['zancount']=$this->params['zancount'];
        $info['collectcount']=$this->params['collectcount'];
        $collegeCou = new CollegeCarousel();
        $result = $collegeCou->save_params($info);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }

    }


    public function validate_fieldcarouup($params){
        if(!isset($params['title']) || empty($params['title'])){
            $this->fail(-1,'标题不能为空');
        }
        if(!isset($params['cid']) || empty($params['cid'])){
            $this->fail(-1,'栏目不能为空');
        }
        $college_column = new CollegeColumn();
        $column = $college_column->find_by_id($params['cid']);
        if(empty($column)){
            $this->fail(-1,'没有找到栏目');
        }
        $college_category = new CollegeCategories();
        $category_count = $college_category->find_count($params['cid']);
        if($category_count){
            if(!isset($params['cateid']) || empty($params['cateid'])){
                $this->fail(-1,'请选择栏目');
            }
        }
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->fail(-1,'请选择类型');
        }else{
            if($this->params['type'] != 3){
                if(!isset($this->params['type']) || empty($this->params['type'])){
                    $this->fail(-1,'请选择封面图');
                }
            }
        }
        if(!isset($params['sort']) || empty($params['sort'])){
            $this->fail(-1,'请填写排序');
        }
        if(!isset($params['content']) || empty($params['content'])){
            $this->fail(-1,'请填写内容');
        }
    }


    public function caroudelAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'内容id不能为空');
        }
        $collegeCarousel = new CollegeCarousel();
        $info = $collegeCarousel->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该分类不存在，或已被删除');
        }
        $result = $collegeCarousel->findFirstById($this->params['id'])->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'删除失败');
        }
    }


    public function caroudetailAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'内容id不能为空');
        }
        $collegeCarousel = new CollegeCarousel();
        $info = $collegeCarousel->find_by_id($this->params['id']);
        if(empty($info)){
            $this->fail(-1,'该分类不存在，或已被删除');
        }
        $this->ok($info);
    }


}
